package dj.common.support;

import com.dj.common.response.CentralListResponse;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <br>
 * <p>中心部件响应数据列表转换处理handle</p>
 *
 * <br>
 * @author ZJG
 * @version 1.0
 * @date 2021/7/10 下午3:04
 */
public final class ResponseListHandler {

    /**
     * 处理成功
     * 返回无提示信息的中心部件response对象
     * {@link CentralListResponse}
     * @return
     */
    public static <TDest> CentralListResponse<TDest> success() {
        final CentralListResponse<TDest> response = new CentralListResponse<>();
        return response;
    }

    /**
     * 处理成功
     * 返回无提示信息的中心部件response对象
     * {@link CentralListResponse}
     * @return
     */
    public static <TDest> CentralListResponse<TDest> success(String message) {
        final CentralListResponse<TDest> response = new CentralListResponse<>();
        response.setMsg(message);
        return response;
    }

    /**
     * 处理失败
     * 返回无提示信息的中心部件response对象
     * {@link CentralListResponse}
     *
     * @return
     */
    public static <TDest> CentralListResponse<TDest> fail() {
        final CentralListResponse<TDest> response = new CentralListResponse<>();
        response.setCode(500);
        response.setMsg("request is failure.");
        return response;
    }

    /**
     * 处理失败
     * 返回带有提示msg的中心部件response对象
     * {@link CentralListResponse}
     *
     * @param message
     * @return
     */
    public static <TDest> CentralListResponse<TDest> fail(String message) {
        final CentralListResponse<TDest> response = new CentralListResponse<>();
        response.setCode(500);
        response.setMsg(message);
        return response;
    }

    /**
     * 处理失败
     * 返回带有提示code&msg的中心部件response对象
     * {@link CentralListResponse}
     *
     * @param code
     * @param message
     * @return
     */
    public static <TDest> CentralListResponse<TDest> fail(int code, String message) {
        final CentralListResponse<TDest> response = new CentralListResponse<>();
        response.setCode(code);
        response.setMsg(message);
        return response;
    }

    /**
     * 返回中心部件response对象
     * {@link CentralListResponse}
     *
     * @param result
     * @param <TDest>
     * @return
     */
    public static <TDest> CentralListResponse<TDest> toResponse(List<TDest> result) {
        final CentralListResponse<TDest> response = new CentralListResponse<>();
        response.setData(emptyIfNull(result));
        return response;
    }

    /**
     * 转为单响应数据对象
     * {@link CentralListResponse}
     *
     * @param result
     * @param converter
     * @param <TSrc>
     * @param <TDest>
     * @return
     */
    public static <TSrc, TDest> CentralListResponse<TDest> toResponse(List<TSrc> result,
                                                                      ResponseConverter<TSrc, TDest> converter) {
        final CentralListResponse<TDest> response = new CentralListResponse<>();
        List<TDest> destList = convert(result, converter);
        response.setData(destList);
        return response;
    }

    /**
     * 由源中心部件response转换到目标中心部件响应数据对象
     * {@link CentralListResponse}
     *
     * @param source
     * @param converter
     * @param <TSrc>
     * @param <TDest>
     * @return
     */
    public static <TSrc, TDest> CentralListResponse<TDest> toListResponse(final CentralListResponse<TSrc> source,
                                                                          final ResponseConverter<TSrc, TDest> converter) {
        return null != source ? toResponse(source.getData(), converter) : new CentralListResponse<TDest>();
    }


    protected static <TDest> List<TDest> emptyIfNull(final List<TDest> collection) {
        return collection == null ? Collections.<TDest>emptyList() : collection;
    }

    protected static <TSrc, TDest> List<TDest> convert(final Collection<TSrc> sources,
                                                       final ResponseConverter<TSrc, TDest> converter) {
        if (null != sources && !sources.isEmpty()) {
            if (null == converter) {
                throw new NullPointerException("程序代码错误，converter转换器为null");
            }
            final List<TDest> result = new ArrayList<>(sources.size());
            for (TSrc s : sources) {
                TDest v = converter.convert(s);
                if (v != null) {
                    result.add(v);
                }
            }
            return result;
        }
        return Collections.emptyList();
    }

}
